#include <stdio.h>
#include <stdlib.h>
#include <glob.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <string.h>

#define NEXTPATHSIZE		1024

int path_noloop(const char *fname)
{
//  fname = path    /a/b/c/d/e/..
	char *pos;

	pos = strrchr(fname,'/');
		
	if(strcmp(pos+1,".") == 0 || strcmp(pos+1,"..") == 0)
		return 0;
	return 1;
}

int64_t mydu(const char *fname)
{	
	int i;
	int64_t sum = 0;
	static struct stat statres;
	static char nextpath[NEXTPATHSIZE];
	glob_t globres;

	if(lstat(fname,&statres) < 0)
	{
		perror("lstat()");
		exit(1);
	}
	
	if(!S_ISDIR(statres.st_mode))
		return statres.st_blocks;

//	is a dir;  asdf/asdf/asdf/asdf

	strncpy(nextpath,fname,NEXTPATHSIZE);	
	strncat(nextpath,"/*",NEXTPATHSIZE);
	if(glob(nextpath,0,NULL,&globres) < 0)
	{
		perror("glob()");
		exit(1);
	}

	strncpy(nextpath,fname,NEXTPATHSIZE);   
        strncat(nextpath,"/.*",NEXTPATHSIZE);
        if(glob(nextpath,GLOB_APPEND,NULL,&globres) < 0)
	{
		perror("glob()");
                exit(1);
	}

	sum = statres.st_blocks;
		
	for(i = 0 ; i < globres.gl_pathc; i++)
	{
		if(path_noloop(globres.gl_pathv[i]))
			sum += mydu(globres.gl_pathv[i]);
	}	

	return sum;
}

int main(int argc,char **argv)
{

	if(argc < 2)
	{
		fprintf(stderr,"Usage....\n");
		exit(1);
	}
	
	
	printf("%lld\n",mydu(argv[1])/2);

	exit(0);
}




